<template>
    <div>
        <!-- 基本信息 -->
        <el-row>
            <el-col :span="24" style="padding: 20px 10px">
                <el-descriptions title="店家信息" direction="vertical" :column="4" border>
                    <el-descriptions-item label="店家ID">{{
                        rowobj.store_id
                    }}</el-descriptions-item>
                    <el-descriptions-item label="店家名称">{{
                        rowobj.store_name
                    }}</el-descriptions-item>
                    <el-descriptions-item label="入驻时间">{{
                        rowobj.entry_time
                    }}</el-descriptions-item>
                    <el-descriptions-item label="省份">
                        {{ rowobj.province }}
                    </el-descriptions-item>
                    <el-descriptions-item label="城市">{{
                        rowobj.city
                    }}</el-descriptions-item>
                    <el-descriptions-item label="区域">{{
                        rowobj.district
                    }}</el-descriptions-item>
                    <el-descriptions-item label="地址">{{
                        rowobj.address
                    }}</el-descriptions-item>
                    <el-descriptions-item label="会员状态">{{
                        rowobj.membership_status
                    }}</el-descriptions-item>
                    <el-descriptions-item label="是否启用广告收益">{{
                        rowobj.is_ad_earning_enabled
                    }}</el-descriptions-item>
                    <el-descriptions-item label="总收益">{{
                        rowobj.total_earnings
                    }}</el-descriptions-item>
                    <el-descriptions-item label="余额">{{
                        rowobj.balance
                    }}</el-descriptions-item>
                    <el-descriptions-item label="广告币数量">{{
                        rowobj.ad_coin_count
                    }}</el-descriptions-item>
                    <el-descriptions-item label="营业时间">{{
                        rowobj.opening_hours
                    }}</el-descriptions-item>
                    <el-descriptions-item label="广告位状态">{{
                        rowobj.ad_slot_status
                    }}</el-descriptions-item>
                    <el-descriptions-item label="会员等级">{{
                        rowobj.membership_level
                    }}</el-descriptions-item>
                </el-descriptions>
            </el-col>
        </el-row>
        <!-- 选项卡 -->
        <el-row type="flex" class="row-bg" justify="space-between">
            <el-col :span="4" style="padding: 0px 20px">
                <el-tabs v-model="activeName" @tab-click="handleClick">
                    <el-tab-pane label="团购" name="first"></el-tab-pane>
                    <el-tab-pane label="菜单" name="second"></el-tab-pane>
                </el-tabs>
            </el-col>
            <el-col :span="2" style="margin-left: 25px;">
                <el-button v-if="activeName == 'first'" @click="addProduct('add')">{{ '添加团购'
                }}</el-button>
                <el-button v-if="activeName == 'second'" @click="addDish('add')">{{ '添加单品'
                }}</el-button></el-col>
        </el-row>
        <!-- 团购 -->
        <el-row v-if="activeName == 'first'">
            <!-- 产品 -->
            <el-col style="padding: 10px">
                <el-table :data="menuData" border fit highlight-current-row style="width: 100%" v-loading="menuLoading">
                    <el-table-column align="center" label="头像">
                        <template slot-scope="{ row }">
                            <img style="width: 50px; height: 50px" :src="row.avatar" alt="" />
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="listing_time" label="上架时间">
                    </el-table-column>
                    <el-table-column align="center" prop="start_time" label="开始时间">
                    </el-table-column>
                    <el-table-column align="center" prop="end_time" label="结束时间">
                    </el-table-column>
                    <el-table-column align="center" prop="validity_period" label="有效期">
                    </el-table-column>
                    <el-table-column align="center" prop="group_buying_name" label="团购名称">
                    </el-table-column>
                    <el-table-column align="center" prop="group_buying_id" label="团购ID">
                    </el-table-column>
                    <el-table-column align="center" prop="status" label="状态">
                    </el-table-column>
                    <el-table-column align="center" prop="price" label="价格">
                    </el-table-column>
                    <el-table-column align="center" prop="rules" label="规则">
                    </el-table-column>
                    <el-table-column align="center" fixed="right" label="操作">
                        <template slot-scope="scope">
                            <el-button type="text" size="small" @click="addProduct('edit', scope.row)">编辑</el-button>
                            <el-button type="text" size="small" @click="productDelBth(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>
        <!--  单品-->
        <el-row type="flex" class="row-bg" justify="space-between" v-if="activeName == 'second'"> <el-col :span="3">
                <el-menu default-active="2" class="el-menu-vertical-demo" @select="changeSidebar">
                    <el-menu-item v-for="(item, index) in category_list" :key="index" :index="item">
                        <span slot="title">{{ item }}</span>
                    </el-menu-item>
                </el-menu>
            </el-col>
            <el-col>
                <el-table :data="singleList" border fit highlight-current-row style="width: 100%" v-loading="singLoading">
                    <el-table-column align="center" label="头像">
                        <template slot-scope="{ row }">
                            <img style="width: 50px; height: 50px" :src="row.avatar" alt="" />
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="dish_name" label="单品名称">
                    </el-table-column>
                    <el-table-column align="center" prop="dish_category" label="单品类目">
                    </el-table-column>
                    <el-table-column align="center" prop="price" label="价格">
                    </el-table-column>
                    <el-table-column align="center" label="入驻时间">
                        <template slot-scope="{ row }">
                            <span>{{ row.listing_time }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="status" label="状态">
                    </el-table-column>
                    <el-table-column align="center" fixed="right" label="操作">
                        <template slot-scope="scope">
                            <el-button type="text" size="small" @click="addDish('edit', scope.row)">编辑</el-button>
                            <el-button type="text" size="small" @click="productDelBth(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                    <!-- <template slot-scope="scope">
                        <el-button type="text" size="small" @click="addProduct('edit', scope.row)">编辑</el-button>
                        <el-button type="text" size="small" @click="productDelBth(scope.row)">删除</el-button>
                    </template> -->
                </el-table>
            </el-col>
        </el-row>
        <el-dialog :title="dialogTitle" :visible.sync="dialogaddProduct">
            <el-form ref="commodity" :model="commodityForm" v-if="activeName == 'first'" :rules="commodityRules">
                <!-- 团购-->
                <el-row style="background: #fff;" type="flex" class="row-bg" justify="space-between">
                    <el-col :span="7">
                        <el-form-item label="团购名称" :label-width="formLabelWidth" prop="group_buying_name">
                            <el-input v-model="commodityForm.group_buying_name" autocomplete="off"
                                placeholder="请输入团购名称"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="15">
                        <el-form-item label="单品列表" :label-width="formLabelWidth" prop="dish_id_list">
                            <el-select style="width: 100%;" multiple v-model="commodityForm.dish_id_list"
                                placeholder="请选择活动区域">
                                <el-option v-for="(item, i) in dishList" :key="i"
                                    :label="`${item.dish_category}-${item.dish_name}-${item.price}元`"
                                    :value="`${item.dish_id}`"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row type="flex" class="row-bg" justify="space-between" style="background: #fff;">
                    <el-col :span="7">
                        <el-form-item label="开始时间" :label-width="formLabelWidth" prop="start_time">
                            <el-date-picker type="date" placeholder="请选择开始时间" v-model="commodityForm.start_time"
                                style="width: 100%;"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="结束时间" :label-width="formLabelWidth" prop="end_time">
                            <el-date-picker type="date" placeholder="请选择结束时间" v-model="commodityForm.end_time"
                                style="width: 100%;"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="价格" :label-width="formLabelWidth" prop="price">
                            <el-input type="number" v-model="commodityForm.price" autocomplete="off"
                                placeholder="请输入价格"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row> <el-col>
                        <el-form-item label="规格" :label-width="formLabelWidth" prop="rules">
                            <el-input style="width: 180px;" v-model="commodityForm.rules" autocomplete="off"
                                placeholder="请输入规格"></el-input>
                        </el-form-item>
                    </el-col></el-row>

                <el-row class="spanForm" v-if="rowType == 'add'">
                    <!-- <span>商品图片</span> -->
                    <el-form-item label="商品图片" :label-width="formLabelWidth" prop="avatar">
                        <Kupload ref="myUpload" @callbackFile="callbackFile" typeName="first" />
                    </el-form-item>
                </el-row>

            </el-form>
            <!-- 单品 -->
            <el-form ref="single" :model="singleform" v-if="activeName == 'second'" :rules="singleRules">
                <el-row type="flex" class="row-bg" justify="space-between" style="background: #fff;">
                    <el-col :span="7">
                        <el-form-item label="单品名称" :label-width="formLabelWidth" prop="dish_name">
                            <el-input v-model="singleform.dish_name" autocomplete="off" placeholder="请输入单品名称"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="单品类目" :label-width="formLabelWidth" prop="dish_category">
                            <el-input v-model="singleform.dish_category" autocomplete="off"
                                placeholder="请输入单品类目"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="价格" :label-width="formLabelWidth" prop="price">
                            <el-input v-model="singleform.price" autocomplete="off" placeholder="请输入价格"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row class="spanForm" v-if="singleType == 'add'">
                    <el-form-item label="商品图片" :label-width="formLabelWidth" prop="avatar">
                        <Kupload ref="myUpload" @callbackFile="callbackFile" typeName="second" />
                    </el-form-item>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogaddProduct = false">取 消</el-button>
                <el-button type="primary" @click="editConfirmation">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
  
<script>
import { editSingle, del_group, productPage, singleItem, addcommodity, addSingle, add_group } from "@/api/commodity"
import tableRouter from "@/router/modules/table";
import { timestampToTime, containsComma } from "@/utils"
import Kupload from "@/components/Upload"
export default {
    components: { Kupload },
    data() {
        return {
            rowobj: {},
            activeName: "first", // 选项卡
            dialogaddProduct: false,
            commodityForm: {},//商品添加数据
            formLabelWidth: '80px',
            singleList: [],//单品数据
            dialogTitle: '添加商品',
            singleform: {},//单品添加数据
            menuData: [],//菜单数据
            publicWelfareUrl: [],//头像数据
            fileList: [],//图片地址数据
            dishList: [],//单品下拉数据
            category_list: [],//单品导航栏数据
            dish_category: JSON.stringify(""),//单品导航栏数据
            singLoading: false,//单品加载动画
            menuLoading: false,//团购加载动画
            commodityRules: {
                group_buying_name: [{ required: true, trigger: 'blur', message: '团购名称不能为空' }],
                dish_id_list: [
                    { required: true, message: '单品列表不能为空', trigger: 'change' }
                ],
                start_time: [{ required: true, trigger: 'blur', message: '开始时间不能为空' }],
                end_time: [{ required: true, trigger: 'blur', message: '结束时间不能为空' }],
                price: [{ required: true, trigger: 'blur', message: '价格不能为空' }],
                rules: [{ required: true, trigger: 'blur', message: '规格不能为空' }],
                avatar: [{ required: true, trigger: 'blur', message: '商品图片为空' }],
            },
            singleRules: {
                dish_name: [{ required: true, trigger: 'blur', message: '店家id不能为空' }],
                dish_category: [{ required: true, trigger: 'blur', message: '单品名称不能为空' }],
                group_buying_name: [{ required: true, trigger: 'blur', message: '单品类目不能为空' }],
                price: [{ required: true, trigger: 'blur', message: '价格不能为空' }],
                avatar: [{ required: true, trigger: 'blur', message: '商品图片为空' }],
            },
            rowType: '',
            singleType: '',//商品
        };
    },
    methods: {
        // 文件状态改变时的回调钩子
        callbackFile({ file, typeName, publicWelfareUrl }) {
            this.publicWelfareUrl.push(publicWelfareUrl)
            //团购新增图片回调
            if (typeName == "first") {
                console.log(file, 'file')
                this.commodityForm.avatar = file;
                // console.log(this.commodityForm, 'this.commodityForm.avata')
            } else { //单品新增图片回调
                this.singleform.avatar = file;
                console.log(this.singleform, 'this.commodityForm.avata')
            }
        },
        handleClick() {
            // Object.assign(this.$data, this.$options.data())
            this.$nextTick(() => {
                // 在这里获取 this.$refs.dom
                this.$refs.myUpload ? this.$refs.myUpload.cleanimg() : null
            })
            if (this.activeName == 'first') {
                this.singleform = {}
                this.dialogTitle = '添加商品'
                this.queryProductPage()
            } else {
                this.commodityForm = {}
                this.dialogTitle = '添加单品'
                this.querysingleItem()
            }
        },
        //单品列表
        querysingleItem() {
            this.singLoading = true;
            let params = {
                dish_category: this.dish_category,
                store_id: this.rowobj.store_id
            }
            singleItem(params).then(res => {
                res.result.forEach(item => {
                    item.listing_time = timestampToTime(item.listing_time)
                });
                this.singleList = res.result
                this.category_list = res.category_list
                this.singLoading = false;
            })
        },
        //团购数据
        queryProductPage() {
            this.menuLoading = true;
            let params = {
                page: 1,
                store_id: this.rowobj.store_id
            }
            productPage(params).then(res => {
                res.result.forEach(item => {
                    item.start_time = timestampToTime(item.start_time)
                    item.end_time = timestampToTime(item.end_time)
                    item.listing_time = timestampToTime(item.listing_time)
                });
                this.menuData = res.result
                this.menuLoading = false;
            })
        },
        //团购编辑
        addProduct(type, row) {
            this.rowType = type;
            if (type == 'edit') {
                console.log(containsComma(row.dish_id_list), 'row.dish_id_list.split')
                // if (containsComma(row.dish_id_list)) {
                //     console.log(11111111, 'row.dish_id_list.split')
                //     row.dish_id_list = row.dish_id_list.split(',')
                // }
                this.commodityForm = row;
            } else if ('add') {
                add_group({ store_id: this.rowobj.store_id }).then(res => {
                    this.dishList = res.result
                })
                Object.keys(this.commodityForm).forEach(key => { this.commodityForm[key] = '' })
            }
            this.dialogaddProduct = true;
        },
        //单品编辑
        addDish(type, row) {
            this.singleType = type;
            if (type == 'edit') {
                this.singleform = row;
            } else if ('add') {
                Object.keys(this.singleform).forEach(key => { this.singleform[key] = '' })
            }
            this.dialogaddProduct = true;
        },
        //删除团购
        productDelBth({ group_buying_id }) {
            let params = {
                store_id: this.rowobj.store_id,
                id: group_buying_id,
                code: '团购'
            }
            del_group(params).then(res => {
                this.queryProductPage()
            })
        },
        //确认框
        editConfirmation() {
            //商品
            if (this.activeName == "first") {
                this.$refs.commodity.validate(valid => {
                    if (this.rowType == 'add') {
                        if (valid || this.publicWelfareUrl.length > 0) {
                            this.commodityForm.start_time = timestampToTime(this.commodityForm.start_time)
                            this.commodityForm.end_time = timestampToTime(this.commodityForm.end_time)
                            var formData = new FormData();
                            for (let i in this.commodityForm) {
                                formData.append(i, this.commodityForm[i])
                            }
                            formData.append('store_id', this.rowobj.store_id)
                            addcommodity(formData).then(res => {
                                this.dialogaddProduct = false;
                                this.queryProductPage()
                            })
                        }
                    } else {
                        //修改接口
                        delete this.commodityForm.avatar;
                        let params = {
                            store_id: this.rowobj.store_id,
                            id: this.commodityForm.group_buying_id,
                            code: '团购',
                            data: { ...this.commodityForm }
                        }
                        editSingle(params)
                    }
                })
            } else {
                console.log(this.singleType, ' this.singleType')
                this.$refs.single.validate(valid => {
                    if (valid || this.publicWelfareUrl.length > 0) {
                        if (this.singleType == 'add') {
                            var formData = new FormData();
                            for (let i in this.singleform) {
                                formData.append(i, this.singleform[i])
                            }
                            formData.append('store_id', this.rowobj.store_id)
                            //单品
                            addSingle(formData).then(res => {
                                if (res.data.code == 15) {
                                    this.dialogaddProduct = false;
                                    this.queryProductPage()
                                    this.$message({
                                        type: 'success',
                                        showClose: true,
                                        message: '添加成功'
                                    });
                                } else {
                                    this.$message.error(`${res.data.code}`);
                                }

                            })
                        } else {
                            delete this.singleform.avatar;
                            let params = {
                                store_id: this.rowobj.store_id,
                                id: this.singleform.group_buying_id,
                                code: '商品',
                                data: { ...this.singleform }
                            }
                            editSingle(params)
                        }
                    }
                })
            }
        },
        changeSidebar(key, i) {
            this.dish_category = key;
            this.querysingleItem()//调用单品列表
        },
    },
    mounted() {
        Object.assign(this.$data, this.$options.data())
        this.rowobj = this.$route.query;
        this.queryProductPage()

    },
};
</script>
  
<style lang="scss" scoped>
.spanForm {
    display: flex;
    align-items: center;
    text-align: right;
    vertical-align: middle;
    font-size: 14px;
    color: #606266;
    padding: 0 12px 0 0;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;

    span {
        margin-right: 10px;
        font-weight: 600;
    }
}



.avatar {
    width: 178px;
    height: 178px;
    display: block;
}

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    -webkit-appearance: none !important;
    margin: 0;
}

.el-row {
    // margin-bottom: 10px;

    &:last-child {
        margin-bottom: 0;
    }
}

.el-col {
    border-radius: 4px;
}

.bg-purple-dark {
    background: #99a9bf;
}

.bg-purple {
    background: #d3dce6;
}

.bg-purple-light {
    background: #e5e9f2;
}

.grid-content {
    border-radius: 4px;
    min-height: 36px;
}

.row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
}

.el-menu-vertical-demo {
    border-right: none;
}

// el-tabs {
//   display: flex;
//   align-items: center;
//   justify-items: center;
// }
</style>
